HAVE_ENESIM=$(shell pkg-config --exists enesim 2>/dev/null && echo 'yes')
HAVE_EMAGE=$(shell pkg-config --exists emage 2>/dev/null && echo 'yes')
HAVE_ETEX=$(shell pkg-config --exists etex 2>/dev/null && echo 'yes')
HAVE_GTK=$(shell pkg-config --exists gtk+-2.0 2>/dev/null && echo 'yes')

CFLAGS = -g
LIBS =
LEX = flex
YACC = bison -y
YFLAGS = -d

ifeq ($(HAVE_ENESIM),yes)
CFLAGS += $(shell pkg-config --cflags enesim)
LIBS += $(shell pkg-config --libs enesim) -lequanime
endif

ifeq ($(HAVE_EMAGE),yes)
CFLAGS += $(shell pkg-config --cflags emage) -DHAVE_EMAGE
LIBS += $(shell pkg-config --libs emage)
endif

ifeq ($(HAVE_ETEX),yes)
CFLAGS += $(shell pkg-config --cflags etex) -DHAVE_ETEX
LIBS += $(shell pkg-config --libs etex)
endif

ifeq ($(HAVE_GTK),yes)
CFLAGS += $(shell pkg-config --cflags gtk+-2.0)
LIBS += $(shell pkg-config --libs gtk+-2.0)
endif

BINS  = enesim_parser enesim_demo
all: $(BINS)

enesim_parser_SRCS = \
enesim_parser.c

enesim_parser_OBJS = $(enesim_parser_SRCS:.c=.o) scan.o parse.o
enesim_parser: $(enesim_parser_OBJS)
	$(RM) $@
	$(CC) $(LIBS) $(CFLAGS) $(LDFLAGS) $(enesim_parser_OBJS) -o $@

scan.o: scan.l parse.c
parse.o: parse.y

enesim_demo_RENDERERS = \
grid.c \
checker.c \
stripes.c \
perlin.c \
circle.c \
ellipse.c \
rect.c \
fancy_button01.c \
fancy_progress01.c

ifeq ($(HAVE_EMAGE),yes)
enesim_demo_RENDERERS += \
raddist.c \
dispmap.c
endif

ifeq ($(HAVE_ETEX),yes)
enesim_demo_RENDERERS += \
etex.c \
fancy_button02.c
endif

enesim_demo_SRCS = \
enesim_demo.c \
fancy/button01.c \
fancy/progress01.c \
$(enesim_demo_RENDERERS)

ifeq ($(HAVE_ETEX),yes)
enesim_demo_SRCS += fancy/button02.c
endif

enesim_demo_OBJS = $(enesim_demo_SRCS:.c=.o)
enesim_demo: enesim_parser $(enesim_demo_OBJS)
	$(RM) $@
	$(CC) $(LIBS) $(CFLAGS) $(LDFLAGS) $(enesim_demo_OBJS) -o $@

.desc.c:
	./enesim_parser < $< > $@
.c.o:
	$(CC) $(FLAGS) $(CFLAGS) -c $< -o $@
clean::
	rm -rf $(BINS) *.o *~ y.tab.c y.tab.h scan.c parse.c $(enesim_demo_RENDERERS)

.SUFFIXES: .desc
